package com.gitee.huanminabc.utils_tools.spring_base.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 *Springboot动态获取ioc容器bean对象工具类

 **/
@Component("springContextUtil")
public class SpringContextUtil implements ApplicationContextAware {

    /**
     *       * 上下文对象实例
     *
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * 获取Spring上下文
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取Bean
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        try {
            return getApplicationContext().getBean(name);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 通过class获取Bean
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        try {
            return getApplicationContext().getBean(clazz);
        } catch (Exception e) {
            return null;
        }
    }
    //通过class获取Beans  返回与给定对象类型（包括子类）  如果没有匹配的 Bean 实例，则返回空 Map。
    public static <T> Map<String, T> getBeans(Class<T> clazz) {
        try {
            return getApplicationContext().getBeansOfType(clazz);
        } catch (Exception e) {
            return new HashMap<>();
        }
    }

    //获取全部添加指定注解的bean
    public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
        try {
            return getApplicationContext().getBeansWithAnnotation(annotationType);
        } catch (Exception e) {
            return new HashMap<>();
        }
    }

    /**
     * 通过name,以及Clazz返回指定的Bean
     *
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        try {
            return getApplicationContext().getBean(name, clazz);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     *
     * @param key  配置文件的key   比如spring.application.name
     * @return  返回value
     */
    public static String getConfig(String key) {
       return Optional.ofNullable(getApplicationContext().getEnvironment().getProperty(key)).orElse("");
    }
    
    //获取bean的名字
    public static String[] getBeanNamesForType(Class<?> type) {
        return getApplicationContext().getBeanNamesForType(type);
    }

    //重新加载类到容器，这主要用于类字节码修改后，重新加载到容器
    //beanName  bean的名字 需要重新加载的bean
    //clazz  需要重新加载的类 , 因为加载器加载的类是不一样的，所以需要传入类
    public static void reloadClass(String beanName,Class<?> clazz) {
        Object bean = getApplicationContext().getBean(beanName);
        //销毁bean
        getApplicationContext().getAutowireCapableBeanFactory().destroyBean(bean);
        //重新创建bean
        getApplicationContext().getAutowireCapableBeanFactory().createBean(clazz);
    }




}